package com.angelababy.goods.controller;

import com.angelababy.entity.Page;
import com.angelababy.entity.PageResult;
import com.angelababy.entity.Result;
import com.angelababy.entity.StatusCode;
import com.angelababy.goods.pojo.Brand;
import com.angelababy.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * * * 查询所有品牌数据  * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", brandList);
    }

    /**
     * 根据ID查询品牌数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Brand brand = brandService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", brand);
    }

    /**
     * 新增品牌数据 *
     *
     * @param brand
     * * @return
     */
    @PostMapping
    public Result add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /***
     * * 修改品牌数据
     * @param brand
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Brand brand, @PathVariable("id") Integer id) {
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /*** *
     *  根据ID删除品牌数据
     *  @param id
     *  @return
     *
     *  */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable("id") Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     *  * 多条件搜索品牌数据
     *  * @param searchMap
     *  * @return
     *  */
    @GetMapping(value = "/search" )
    public Result findList(@RequestParam Map searchMap){
        List<Brand> list = brandService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * * 分页搜索实现
     * * @param page
     * * @param size
     * * @return
     * */
//    @GetMapping(value = "/search/{page}/{size}" )
//    public Result findPage(@PathVariable int page, @PathVariable int size){
//         Page<Brand> pageList = brandService.findPage(page, size);
//         PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
//         return new Result(true,StatusCode.OK,"查询成功",pageResult);
//     }

    /***
     * * 分页搜索实现
     * * @param searchMap
     * * @param page
     * * @param size
     * * @return
     * */
//    @GetMapping(value = "/search/{page}/{size}" )
//    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size){
//        Page<Brand> pageList = brandService.findPage(searchMap, page, size);
//        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
//        return new Result(true,StatusCode.OK,"查询成功",pageResult);
//    }

    //根据分类名称查询品牌列表
    @GetMapping("/category/{category}")
    public Result findListByCategoryName(@PathVariable String category){
        List<Map> brandList = brandService.findListByCategoryName(category);
        return new Result(true,StatusCode.OK,"查询成功",brandList);
    }
}

